package com.tt.task;


import java.lang.reflect.Method;

/**
 * @Title: MethodInvocationTask
 * @Author zl
 * @Package com.oberyun.zhgc.datascreen.config
 * @Date 2025/7/29 10:37
 * @description: 定时任务执行线程
 */

public class MethodInvocationTask implements Runnable {
    private final MethodTaskInfo taskInfo;
    private final TaskExecutionListener listener ;

    public MethodInvocationTask(MethodTaskInfo taskInfo, TaskExecutionListener listener) {
        this.taskInfo = taskInfo;
        this.listener = listener;
    }

    @Override
    public void run() {
        long start = System.currentTimeMillis();
        try {
            listener.beforeExecution(taskInfo);

            Object target = taskInfo.getTarget();
            Method method = target.getClass().getMethod(
                    taskInfo.getMethodName(),
                    taskInfo.getParamTypes()
            );
            Object result = method.invoke(target, taskInfo.getArgs());

            listener.afterSuccess(taskInfo, result, System.currentTimeMillis() - start);
        } catch (Exception e) {
            listener.afterFailure(taskInfo, e, System.currentTimeMillis() - start);
        }
    }
}
